package com.shell.governor.modules.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;	

import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.auth.AuthUser;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.dao.PubDateDao;
import com.shell.governor.modules.po.PubDate;
import com.shell.governor.modules.service.PubDateService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

@Service
public class PubDateServiceImpl extends PersistentServiceImpl<PubDate> implements PubDateService {
	@Autowired
	private PubDateDao pubDateDao;

	public List<PubDate> queryHoliday(String startDate, String endDate) {
		Example example = new Example(PubDate.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("isHoliday", "1");
		if (StringUtil.isNotEmpty(startDate)) {
			criteria.andGreaterThanOrEqualTo("dateValue", startDate);
		}
		if (StringUtil.isNotEmpty(endDate)) {
			criteria.andLessThanOrEqualTo("dateValue", endDate);
		}
		List<PubDate> holidays = this.pubDateDao.selectByExample(example);
		return holidays;
	}

	public RetMsg setDateHolidayYesOrNo(PubDate date) {
		String dateStr = date.getDateValue().replaceAll("\\-", "").substring(0, 8);
		String now = SysDateUtil.getSystemDateTime();

		AuthUser user = CommonDataUtil.getCurUser();
		date.setDateId(SystemUtil.getUUID());
		date.setDateValue(dateStr);
		date.setCreateTime(now);
		date.setCreatorId(user.getUserId());
		int retflag = save(date);
		if (retflag > 0) {
			return new RetMsg("success", "������������������");
		}
		return new RetMsg("error", "����������");
	}

	public RetMsg cancelHoliday(PubDate date) {
		String dateStr = date.getDateValue().replaceAll("\\-", "").substring(0, 8);
		Example example = new Example(PubDate.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("dateValue", dateStr);
		criteria.andEqualTo("isHoliday", date.getIsHoliday());
		List<PubDate> days = this.pubDateDao.selectByExample(example);
		if ((null != days) && (days.size() > 0)) {
			PubDate fdate = (PubDate) days.get(0);
			int retflag = delete(fdate);
			if (retflag > 0) {
				return new RetMsg("success", "����������������������");
			}
		}
		return new RetMsg("error", "����������");
	}

	public boolean isHoliday(String yyyyMMdd) {
		Example example = new Example(PubDate.class);
		Example.Criteria criteria = example.createCriteria();

		criteria.andEqualTo("dateValue", yyyyMMdd);
		List<PubDate> holidays = this.pubDateDao.selectByExample(example);
		if ((null != holidays) && (holidays.size() > 0)) {
			PubDate pdate = (PubDate) holidays.get(0);
			if ("1".equals(pdate.getIsHoliday())) {
				return true;
			}
			return false;
		}
		this.logger.warn("����������������isHoliday��" + yyyyMMdd + " ���������� ������������������");

		return false;
	}
}